package com.example.dbaccess;

import java.util.ArrayList;
import java.util.List;

import com.example.yelp.service.beans.Business;
import com.example.yelp.service.beans.Location;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class LocalBitesDB 
{
	//Column Names
	public static final String KEY_ROWID ="_id";
	public static final String KEY_NAME ="restaurant_name";
	public static final String KEY_CATEGORY ="restaurant_category";
	public static final String KEY_ADDRESS ="restaurant_address";
	public static final String KEY_CONTACT_NO ="restaurant_contact_no";
	public static final String KEY_RATING ="restaurant_rating";
	public static final String KEY_DISTANCE ="restaurant_distance";
	public static final String KEY_LATTITUDE ="restaurant_lattitude";
	public static final String KEY_LONGITUDE ="restaurant_longitude";
	
	//DB Variables
	private static final String DATABASE_NAME ="LocalBitesDB";
	private static final String TABLE_NAME ="restaurantTable";
	private static final int DB_VERSION = 1;
	
	private DBHelper helper;
	private final Context ctx;
	private SQLiteDatabase database;
	
	private static class DBHelper extends SQLiteOpenHelper
	{
		public DBHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DB_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL( "CREATE TABLE " + TABLE_NAME + " ("+
					KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
					KEY_NAME + " TEXT NOT NULL, " +
					KEY_ADDRESS +" TEXT NOT NULL, " +
					KEY_CONTACT_NO + " LONG, "+
					KEY_RATING + " DOUBLE, " +
					KEY_DISTANCE + " DOUBLE NOT NULL," +
					KEY_LATTITUDE + " DOUBLE NOT NULL," +
					KEY_LONGITUDE + " DOUBLE NOT NULL);"
					);			
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
	
	}
	
	public LocalBitesDB(Context ct)
	{
		ctx = ct;
	}
	
	public LocalBitesDB open() throws SQLException
	{
		helper = new DBHelper(ctx);
		database = helper.getWritableDatabase();
		return this;
	}
	
	public void close()
	{
		helper.close();
	}

	public long addData(String name,String address, 
			String displayPhone, double rating, double distance,
			double latitude, double longitude) {
		
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, name);
		cv.put(KEY_ADDRESS, address );
		cv.put(KEY_CONTACT_NO, displayPhone);
		cv.put(KEY_DISTANCE, distance);
		cv.put(KEY_RATING, rating);
		cv.put(KEY_LATTITUDE, latitude);
		cv.put(KEY_LONGITUDE, longitude);
		
		return database.insert(TABLE_NAME, null, cv);		
	}

	public void deleteData()
	{
		database.delete(TABLE_NAME, "1", null);
	}
	public List<Business> getRestaurantData() {
		String [] columns = new String[] {KEY_ROWID, KEY_NAME,KEY_ADDRESS,KEY_CONTACT_NO,KEY_DISTANCE,KEY_LATTITUDE,KEY_LONGITUDE,KEY_RATING};
		Cursor cur = database.query(TABLE_NAME, columns, null, null, null, null, null);
		List<Business> result = new ArrayList<Business>();
				
		for(cur.moveToFirst(); !cur.isAfterLast();cur.moveToNext())
		{
			Business data = new Business();
			data.setName(cur.getString(cur.getColumnIndex(KEY_NAME)));
			data.setDisplayAddress(cur.getString(cur.getColumnIndex(KEY_ADDRESS)));
			data.setRating(cur.getDouble(cur.getColumnIndex(KEY_RATING)));
			data.setDisplayPhone(cur.getString(cur.getColumnIndex(KEY_CONTACT_NO)));
			data.setDistance(cur.getDouble(cur.getColumnIndex(KEY_DISTANCE)));
			data.setLattitude(cur.getDouble(cur.getColumnIndex(KEY_LATTITUDE)));
			data.setLongitude(cur.getDouble(cur.getColumnIndex(KEY_LONGITUDE)));
			result.add(data);			
		}
		
		return result;
	}
	
}
